package com.example.textswitchtestdemo01.util;

import android.util.Log;

import com.example.textswitchtestdemo01.bean.AllProductionLine;
import com.example.textswitchtestdemo01.bean.Material;
import com.example.textswitchtestdemo01.bean.NewsInfo;
import com.example.textswitchtestdemo01.bean.UserAppointment;
import com.example.textswitchtestdemo01.bean.UserNormalCarStore;

import okhttp3.FormBody;
import okhttp3.MediaType;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtil {


    public static final String IP = "192.168.1.103";

    static final Retrofit RETROFIT = new Retrofit.Builder()
            .baseUrl("http://" + IP + ":8085/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    static final RetrofitAPI RETROFIT_API = RETROFIT.create(RetrofitAPI.class);

    static final MediaType MEDIA_TYPE = MediaType.parse("application/x-www-form-urlencoded");
    public static final String TAG = "RetrofitUtil";


    public static void getUserNormalCarStore(Callback<UserNormalCarStore> callback) {
        Call<UserNormalCarStore> call = RETROFIT_API.getUserNormalCarStoreInfo();
        call.enqueue(callback);
    }

    public static void getAllUserAppointment(Callback<UserAppointment> callback) {
        Call<UserAppointment> call = RETROFIT_API.getAllUserAppointment();
        call.enqueue(callback);
    }

    public static void addUserAppointment(UserAppointment.DataBean bean, Callback<UserAppointment> callback) {
        if (bean != null) {
            FormBody body = new FormBody.Builder()
                    .add("userWorkId", bean.getUserWorkId() + "")
                    .add("userAppointmentName", bean.getUserAppointmentName())
                    .add("content", bean.getContent())
                    .add("type", bean.getType() + "")
                    .add("carId", bean.getCarId() + "")
                    .add("time", bean.getTime() + "")
                    .add("num", bean.getNum() + "")
                    .add("gold", bean.getGold() + "")
                    .add("engine", bean.getEngine())
                    .add("speed", bean.getSpeed())
                    .add("wheel", bean.getWheel())
                    .add("control", bean.getControl())
                    .add("brake", bean.getBrake())
                    .add("hang", bean.getHang())
                    .add("color", bean.getColor())
                    .build();

            Call<UserAppointment> call = RETROFIT_API.addUserAppointment(body);
            call.enqueue(callback);

        } else {
            Log.d(TAG, "addUserAppointment: null");
        }


    }

    public static void addAppointment(UserAppointment.DataBean bean, Callback<UserAppointment> callback) {
        Log.d(TAG, bean.toString());
        Call<UserAppointment> call = RETROFIT_API.addAppointment(bean);
        call.enqueue(callback);
    }

    public static void getAllProductionLine(Callback<AllProductionLine> callback) {
        Call<AllProductionLine> call = RETROFIT_API.getAllProductionLine();
        call.enqueue(callback);
    }

    public static void addProductionLine(AllProductionLine.DataBean bean, Callback<AllProductionLine> callback) {
        if (bean != null) {
            FormBody body = new FormBody.Builder()
                    .add("userWorkId", bean.getUserWorkId() + "")
                    .add("stageId", bean.getStageId() + "")
                    .add("productionLineId", bean.getProductionLineId() + "")
                    .add("type", bean.getType() + "")
                    .add("position", bean.getPosition() + "")
                    .add("isAI", bean.getIsAI() + "")
                    .build();
            Call<AllProductionLine> allProductionLineCall = RETROFIT_API.addProductionLine(body);
            allProductionLineCall.enqueue(callback);

        } else {
            Log.d(TAG, "addProductionLine: null");
        }
    }

    public static void getAllMaterial(Callback<Material> callback) {
        RETROFIT_API.getAllMaterial().enqueue(callback);
    }

    public static void getAllNew(Callback<NewsInfo> callback) {
        RETROFIT_API.getAllNews().enqueue(callback);

    }

    public static void getNewsById(int id, Callback<NewsInfo> callback) {
        FormBody body = new FormBody.Builder().add("id", id+"").build();
        Call<NewsInfo> call = RETROFIT_API.getNewsById(body);
        call.enqueue(callback);
    }
}
